#!/bin/sh
# PCP QA Test No. 1187
# Exercise pcp-dstat archive mode and CSV functionality.
# - live metrics part has been split off to qa/1636
#
# Copyright (c) 2018-2021 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

DSTAT="$PCP_BINADM_DIR/pcp-dstat"
test -f "$DSTAT" || _notrun "$DSTAT is not installed, skipped"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
username=`id -u -n`
trap "_cleanup; exit \$status" 0 1 2 3 15

# read from the pcp-dstat archive with various dstat options
#
dstat_archive()
{
    archive="$1"
    message="$2"
    shift
    shift
    options="$@"

    echo "$message"

    pcp_options="pcp -z --origin=+0.1 --archive $archive"
    $pcp_options dstat --time $options 1 3 >$tmp.out 2>$tmp.err

    echo "=== std out"
    cat $tmp.out
    echo "=== std err"
    cat $tmp.err
    echo "=== done" && echo
}

dstat()
{
    dstat_archive "$here/archives/pcp-dstat" "$@"
}

dstat_amdgpu()
{
    dstat_archive "$here/archives/amdgpu" "$@"
}

dstat_nvidiagpu()
{
    dstat_archive "$here/archives/nvidiagpu" "$@"
}

filter_info()
{
    sed \
	-e "/^$(printf '\t')/d" \
	-e "/^.GCC /d" \
	-e "s/^Python .*/Python VERSION/g" \
	-e "s,$PCP_SYSCONF_DIR,/etc/pcp,g" \
	-e "s,pcp-dstat $PCP_VERSION,pcp-dstat VERSION,g" \
	-e "s/\"User:\",\"$username\"/\"User:\",\"USER\"/g" \
	-e "s/\"Date:\",\".*\"/\"Date:\",\"DATE\"/g" \
	-e "s,-o $tmp.csv,-o TMP.csv,g" \
    #end
}

# real QA test starts here
export TERM=ansi
dstat "All defaults to EOL"
dstat "Per CPU" --cpu -C 1,2,total --cpu-use
dstat "CPU" --cpu-adv
dstat_nvidiagpu "NVIDIA GPU" --nvidia-gpu
dstat_amdgpu "AMD GPU" --amd-gpu -G 0
dstat "Per Disk" --disk -D vda,total --disk-wait
dstat "All Disks" --disk --full
dstat "Zero Disks" --all -D --full
dstat "Disk" --io
dstat "Kernel" --ipc --sys --utmp
dstat "Memory" --mem-adv --swap
dstat "Per Interface" --net -N enp0s25,wlp3s0,tun0
dstat "Per Interface in bits" --net -N enp0s25 --bits
dstat "Per Interface packets" --net-packets -N enp0s25,wlp3s0,tun0
dstat "All Interfaces" --net --full
dstat "Network" --socket --tcp
dstat "Processes" --proc
dstat "Filesystem" --aio --fs --lock
dstat "Filesystem alias" --aio --filesystem --lock
dstat "Duplicate arguments" -ddd
dstat "Overlapping arguments" -dam

dstat "Version" --version | filter_info
dstat "Plugins" --list | filter_info

# No stdout/stderr redirecting
echo "Piping"
pcp -z --archive $here/archives/pcp-dstat dstat --time 1 3 | head -n 3
echo "=== done" && echo

# No existing archive
echo "Missing archive"
pcp -z --archive /nosuchfile dstat --version
echo "=== done" && echo

dstat "CSV stdout" --io --tcp --aio -o $tmp.csv | filter_info
echo "CSV contents" | tee -a $seq_full
cat $tmp.csv | tee -a $seq_full | filter_info
echo "CSV complete" | tee -a $seq_full

dstat "CSV instances" --cpu -C 1,2,total --cpu-use -o $tmp.csv2 | filter_info
echo "CSV instances contents" | tee -a $seq_full
cat $tmp.csv2 | tee -a $seq_full | filter_info
echo "CSV instances complete" | tee -a $seq_full

# verify no terminal size effects on CSV output
dstat "CSV terminal default" -tfvnrl -o $tmp.csvterm0 | filter_info
export SAVED_TPUT_COLUMNS=80 SAVED_COLUMNS=$COLUMNS SAVED_LINES=$LINES
test -x /usr/bin/tput && export SAVED_TPUT_COLUMNS=`/usr/bin/tput cols`
# reduce terminal size, output should be unchanged
test -x /usr/bin/stty && /usr/bin/stty cols 40 >/dev/null 2>&1
export COLUMNS=40 LINES=40
dstat "CSV terminal reduced" -tfvnrl -o $tmp.csvterm1 | filter_info
echo "CSV terminal contents" | tee -a $seq_full
cat $tmp.csvterm1 | tee -a $seq_full | filter_info
echo "CSV terminal compared" | tee -a $seq_full
diff $tmp.csvterm0 $tmp.csvterm1 | tee -a $seq_full | filter_info
echo "CSV terminal complete" | tee -a $seq_full
export COLUMNS=$SAVED_COLUMNS LINES=$SAVED_LINES
test -x /usr/bin/stty && /usr/bin/stty cols $SAVED_TPUT_COLUMNS >/dev/null 2>&1
unset SAVED_TPUT_COLUMNS SAVED_COLUMNS SAVED_LINES

# success, -D all done
status=0
exit
